#!/bin/sh

set -e

# =============================================
# Analyzing disk layout
# =============================================
echo "Analyzing disk layout..."
TARGET_DISK="/dev/sda"
BOOT_PART="${TARGET_DISK}1"
ROOT_PART="${TARGET_DISK}2"
VAR_PART="${TARGET_DISK}3"
OPT_PART="${TARGET_DISK}4"

# Remove old partitions
echo "Checking for disk is exist old partitions..."
for PART_NUM in 2 3 4; do
    if parted -s "$TARGET_DISK" print | grep "^ $PART_NUM"; then
        parted -s "$TARGET_DISK" rm "$PART_NUM"
    fi
done
# =============================================
# Create root partition
# =============================================
echo "Creating root partition..."
# Create root partition 
# boot partition size is 258MiB
# root partition is starting from 258MiB to the end of the disk
parted -s "$TARGET_DISK" mkpart primary ext4 258MiB 50GiB
# mkfs for root partition
mkfs.ext4 -F "$ROOT_PART"
echo "Root partition created..."
# =============================================
# Create var partition
# =============================================
echo "Creating var partition..."
parted -s "$TARGET_DISK" mkpart primary ext4 50GiB 55GiB
# mkfs for var partition
mkfs.ext4 -F "$VAR_PART"
echo "var partition created..."
# =============================================
# Create opt partition
# =============================================
echo "Creating opt partition..."
parted -s "$TARGET_DISK" mkpart primary ext4 55GiB 100%
# mkfs for opt partition
mkfs.ext4 -F "$OPT_PART"
echo "opt partition created..."
# =============================================
# Display partition table
echo "Partition Table:"
parted -s "$TARGET_DISK" print
# =============================================
# Install System
# =============================================
echo "Installing system..."
# Unmount boot partition beause system default mount boot partition(/dev/sda1) to initramfs system /root directory
echo "unmounting boot partition..."
umount "$BOOT_PART" || true
# Product directory
PRODUCT_DIR="/tmp/product"

# Create mount point for boot partition
MNT_BOOT="/boot"
# Create mount point boot
if [ ! -d "$MNT_BOOT" ]; then
	mkdir -p "$MNT_BOOT"
fi
# Mount boot partition
echo "Mounting boot partition..."
mount "$BOOT_PART" "$MNT_BOOT"

echo "Extracting System Boot..."
tar -xzf "$PRODUCT_DIR/SysBoot.tar.gz" -C "$MNT_BOOT"

# disk partition UUID info
UUID_BOOT=$(blkid -o export "$BOOT_PART" | grep ^UUID= | cut -d= -f2)
UUID_ROOT=$(blkid -o export "$ROOT_PART" | grep ^UUID= | cut -d= -f2)

# configure GRUB
echo "configure GRUB..."
sed -i \
  -e "s|\(linux /vmlinuz_prod-[^ ]* root=\)/dev/sda1|\1UUID=$UUID_BOOT|" \
  -e "s|\(linux /vmlinuz-[^ ]* root=\)/dev/sda2|\1UUID=$UUID_ROOT|" \
  /boot/grub/grub.cfg

# sync data write to disk
sync

# Create mount point for root partition
MNT_ROOT="/root"
# Create mount point root
if [ ! -d "$MNT_ROOT" ]; then
	mkdir -p "$MNT_ROOT"
fi
# Mount root partition
echo "Mounting root partition..."
mount "$ROOT_PART" "$MNT_ROOT"

echo "Extracting System RootFS..."
tar -xzf "$PRODUCT_DIR/SysRootFS.tar.gz" -C "$MNT_ROOT"

# sync data write to disk
sync

# Create mount point for var partition
MNT_VAR="/var"
# Create mount point var
if [ ! -d "$MNT_VAR" ]; then
	mkdir -p "$MNT_VAR"
fi
# Mount var partition
echo "Mounting var partition..."
mount "$VAR_PART" "$MNT_VAR"

# Create mount point for opt partition
MNT_OPT="/opt"
# Create mount point opt
if [ ! -d "$MNT_OPT" ]; then
	mkdir -p "$MNT_OPT"
fi
# Mount opt partition
echo "Mounting opt partition..."
mount "$OPT_PART" "$MNT_OPT"

# =============================================
# Post-Install Configuration
# =============================================
echo "Configuring system..."
# Basic system setup
chroot "$MNT_ROOT" /bin/bash <<EOF
# configure fstab
echo "configure fstab..."
cat > /etc/fstab <<FSTAB_EOF
$(blkid -o export "$ROOT_PART" | grep ^UUID=) /     ext4 defaults 0 1
$(blkid -o export "$BOOT_PART" | grep ^UUID=) /boot ext4 defaults 0 2
$(blkid -o export "$VAR_PART" | grep ^UUID=) /var ext4 defaults 0 2
$(blkid -o export "$OPT_PART" | grep ^UUID=) /opt     ext4 defaults 0 2
FSTAB_EOF
EOF

# sync data write to disk
sync

# Finish Configuring system
echo "Finish Configuring system..."

exit 0
